package com.example.sxy.mpandroidchart;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import java.util.ArrayList;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // in this example, a LineChart is initialized from xml
        LineChart chart = (LineChart) findViewById(R.id.chart);
        chart.setDescription("description");
//        左右 y 轴对象
        YAxis leftAxis = chart.getAxisLeft();
//        YAxis rightAxis = chart.getAxisRight();
//        是否可见
        leftAxis.setEnabled(true);
//        rightAxis.setEnabled(true);
//        setDrawAxisLine(boolean enabled): 设置轴线是否绘制。
        leftAxis.setDrawAxisLine(true);
//        setStartAtZero(boolean enabled):如果enabled, Y轴始终从0开始。如果disabled，和下面的设置最小值配合使用。
        leftAxis.setEnabled(true);
        ArrayList<Entry> elist1 = new ArrayList<Entry>();
        ArrayList<Entry> elist2 = new ArrayList<Entry>();
        Entry e11 = new Entry(100.00f, 0);//Q1对应x位序为0，即从0开始。
        elist1.add(e11);
        Entry e12 = new Entry(50.00f, 1);//Q2对应x位序为1.
        elist1.add(e12);
        Entry e13 = new Entry(60.00f, 2);
        elist1.add(e13);
        Entry e14 = new Entry(70.00f, 3);
        elist1.add(e14);
//and so on...
        Entry e21 = new Entry(150.00f, 0);
        elist2.add(e21);
        Entry e22 = new Entry(20.00f, 1);
        elist2.add(e22);
//and so on..
        LineDataSet set1 =  new LineDataSet(elist1, "Company 1");
        LineDataSet set2 = new LineDataSet(elist2, "Company2");
        set1.setColors(new int[] {Color.RED, Color.BLACK, Color.RED, Color.BLACK});
        set2.setColors(new int[] {Color.GREEN, Color.BLUE, Color.GREEN, Color.BLUE});
        ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();
        dataSets.add(set1);
        dataSets.add(set2);
        ArrayList<String> xLabels = new ArrayList<String> ();
        xLabels.add("Q1");    xLabels.add("Q2");
        xLabels.add("Q3");   xLabels.add("Q4");
        LineData lineData = new LineData( xLabels, dataSets );
        chart.setData(lineData);
        chart.invalidate();//refresh
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
